package com.atguigu.utils;


import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

//线程池：利用多线程优化dws9，实现异步IO
//单例：懒汉式
public class ThreadPoolUtil {
    private static ThreadPoolExecutor threadPoolExecutor;

    private ThreadPoolUtil() {

    }

    //懒汉式有线程安全问题，需要双重校验
    public static ThreadPoolExecutor getThreadPoolExecutor() {
        // 双重校验(既解决了线程安全问题，又提高了效率)
        if (threadPoolExecutor == null) {
            synchronized (ThreadPoolUtil.class) {
                if (threadPoolExecutor == null) {
                    //有可能两个线程都进来了，就会都判断为null，就会创建两个对象了，所以要加同步锁
                    threadPoolExecutor = new ThreadPoolExecutor(4,
                            20,//最大20个线程
                            5,//维持5min
                            TimeUnit.MINUTES,
                            new LinkedBlockingQueue<>());//工作队列
                }
            }
        }

        return threadPoolExecutor;

    }

}
